package com.sl.biz.modular.workflow.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.sl.biz.modular.ai.controller.domain.request.AiGenerateEntityRequest;
import com.sl.biz.modular.ai.service.AiChatRunBizService;
import com.sl.biz.modular.process.controller.request.SquareLakeProcessResourceAddRequest;
import com.sl.biz.modular.process.controller.response.ProcessVO;
import com.sl.biz.modular.process.controller.response.ProcessVersionVO;
import com.sl.biz.modular.process.dao.domain.dto.ProcessVersionDTO;
import com.sl.biz.modular.process.dao.domain.entity.ProcessConfigDO;
import com.sl.biz.modular.process.dao.domain.entity.ProcessRelationConfigDO;
import com.sl.biz.modular.process.dao.domain.entity.ProcessVersionDO;
import com.sl.biz.modular.process.service.ProcessConfigBizService;
import com.sl.biz.modular.process.service.ProcessRelationConfigBizService;
import com.sl.biz.modular.process.service.ProcessVersionBizService;
import com.sl.biz.modular.process.service.ProcessVersionContentBizService;
import com.sl.biz.modular.workflow.domain.dto.WorkflowContentDTO;
import com.sl.biz.modular.workflow.service.WorkflowContentBizService;
import com.sl.core.domain.dto.SquareLakeLayout;
import com.sl.core.domain.dto.Transformation;
import com.sl.core.domain.vo.ProcessFrontDefineBaseVO;
import com.sl.core.engine.ProcessConfig;
import com.sl.core.engine.utils.TidUtil;
import com.sl.core.utils.JsonUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
@RequiredArgsConstructor
public class WorkflowContentBizServiceImpl implements WorkflowContentBizService {

    private final ProcessRelationConfigBizService relationConfigBizService;
    private final ProcessVersionContentBizService processVersionContentBizService;
    private final ProcessVersionBizService processVersionBizService;
    private final ProcessConfigBizService processConfigBizService;
    private final AiChatRunBizService aiChatRunBizService;

    @Override
    public WorkflowContentDTO getByWorkflowId(WorkflowContentDTO workflowContentDO) {
//        return workflowContentDaoService.getByWorkId(workflowContentDO.getWorkflowId());
        List<ProcessRelationConfigDO> workflow = relationConfigBizService.queryByBizIdAndBizType(workflowContentDO.getWorkflowId(), "WORKFLOW");

        if (CollUtil.isEmpty(workflow)) {
            return null;
        }
        ProcessRelationConfigDO processRelationConfigDO = CollUtil.get(workflow, 0);
        String processVersion = processRelationConfigDO.getProcessVersion();
        String processId = processRelationConfigDO.getProcessId();
        ProcessConfigDO processConfigDO = new ProcessConfigDO();
        processConfigDO.setId(processId);
        ProcessVO processVO = processConfigBizService.getById(processConfigDO);

        ProcessVersionDO processVersionDO = new ProcessVersionDTO();
        processVersionDO.setProcessVersion(processVersion);
        processVersionDO.setProcessId(processId);
        ProcessVersionDTO byProcessIdAndVersion = processVersionBizService.getByProcessIdAndVersion(processVersionDO);
        if (ObjectUtil.isNull(byProcessIdAndVersion)) {
            return null;
        }
        processVersionDO.setId(byProcessIdAndVersion.getId());
        //查询流程
        ProcessVersionVO processVersionVO = processVersionContentBizService.getById(processVersionDO);
        if (ObjectUtil.isNull(processVersionVO)) {
            return null;
        }
        WorkflowContentDTO workflowContentDTO = new WorkflowContentDTO();
        workflowContentDTO.setWorkflowId(workflowContentDO.getWorkflowId());
        workflowContentDTO.setBackFile(processVersionVO.getBackFile());
        workflowContentDTO.setFrontFile(processVersionVO.getFrontFile());

        ProcessConfig processConfig = new ProcessConfig();
        processConfig.setProcessName(processVO.getProcessName());
        processConfig.setProcessId(processVO.getId());
        processConfig.setProcessVersion(byProcessIdAndVersion.getProcessVersion());
        processConfig.setDescription(processVO.getDescription());
        processConfig.setProcessCode(processVO.getProcessCode());
        processConfig.setLogLevelCode(processVO.getLogLevelCode());
        processConfig.setBootThreadCode(processVO.getBootThreadCode());
        processConfig.setPrepareThreadCode(processVO.getPrepareThreadCode());
        processConfig.setExecuteThreadCode(processVO.getExecuteThreadCode());
        processConfig.setPrintStepStatusFlag(true);
        processConfig.setTid(TidUtil.getTid());
        processConfig.setLogStoreTypeCode("db");
        String backFile = processVersionVO.getBackFile();
        if (StrUtil.isBlank(backFile)) {
            return workflowContentDTO;
        }

        SquareLakeLayout bean = JsonUtil.toBean(processVersionVO.getBackFile(), SquareLakeLayout.class);

        Transformation transformation = bean.getTransformation();

        transformation.rebuild();

        String s = bean.toXml();

        processConfig.setXml(s);
        processConfig.setRowSetTag(processVO.getRowSetTag());
        processConfig.setRowSetTagDataSourceId(processVO.getRowSetTagDataSourceId());
        processConfig.setWorkflowFlag(true);
        workflowContentDTO.setProcessConfig(processConfig);
        return workflowContentDTO;
    }

    @Override
    public Boolean save(WorkflowContentDTO workflowContentDO) {

        List<ProcessRelationConfigDO> workflow = relationConfigBizService.queryByBizIdAndBizType(workflowContentDO.getWorkflowId(), "WORKFLOW");

        if (CollUtil.isEmpty(workflow)) {
            return null;
        }
        ProcessRelationConfigDO processRelationConfigDO = CollUtil.get(workflow, 0);
        String processVersion = processRelationConfigDO.getProcessVersion();
        String processId = processRelationConfigDO.getProcessId();
        ProcessVersionDO processVersionDO = new ProcessVersionDTO();
        processVersionDO.setProcessVersion(processVersion);
        processVersionDO.setProcessId(processId);
        ProcessVersionDTO byProcessIdAndVersion = processVersionBizService.getByProcessIdAndVersion(processVersionDO);
        if (ObjectUtil.isNull(byProcessIdAndVersion)) {
            return null;
        }
        processVersionDO.setId(byProcessIdAndVersion.getId());
        //查询流程
        SquareLakeProcessResourceAddRequest squareLakeProcessResourceAddRequest = new SquareLakeProcessResourceAddRequest();
        squareLakeProcessResourceAddRequest.setId(processVersionDO.getId());
        squareLakeProcessResourceAddRequest.setFrontFile(JsonUtil.toMap(workflowContentDO.getFrontFile()));
        processVersionContentBizService.saveXml(squareLakeProcessResourceAddRequest);
        return Boolean.TRUE;
    }

    @Override
    public ProcessFrontDefineBaseVO aiGenerateProcessFile(AiGenerateEntityRequest aiGenerateEntityRequest) {
        aiGenerateEntityRequest.setPrefixKeywords("我有一个流程驱动框架，他的流程图的实体定义是我给你的，step里面的name是唯一的并且跟hop里面的from和to是对应的，并且step的里面的type字段只能有[JsonInput,JsonOutput,Groovy],id字段是通过雪花算法生成的，下面是我对你需要帮我画出流程图的描述。");
        ProcessFrontDefineBaseVO squareLakeLayout = aiChatRunBizService.execute(aiGenerateEntityRequest, ProcessFrontDefineBaseVO.class);
        return squareLakeLayout;
    }

}
